/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots;

import java.io.Serializable;
import java.util.HashMap;

import lime2.Location;

/**
 * Information about a tuple type, which will be exchanged among LimeServers.
 * For now, it stores the total write and update counts since LimeServer's 
 * start.
 * @author Xing Jiankuan
 *
 */
public class TupleTypeInfo implements Serializable {

	private static final long serialVersionUID = -4494249870974695939L;
	
	private long outCount = 0;
	private long updateCount = 0;
	private long delUpdateCount = 0;
	private transient long readCount = 0;
	
	private TupleType type;
	private String TSName;
	private Location location = Location.HERE;

	private long takeCount = 0;
	
	public long delTakeCount = 0;
	public long estimatedNum=0;
	
	private static HashMap<String ,HashMap<TupleType,TupleTypeInfo>>
	   maps = new HashMap<String ,HashMap<TupleType,TupleTypeInfo>>();
	
	private TupleTypeInfo(String tsName,TupleType type) {
		this.type = type;
		this.TSName = tsName;
	}
	
	public static TupleTypeInfo[] getTypeRecordList(String tsName)
	{
		HashMap<TupleType,TupleTypeInfo> map = 
			maps.get(tsName);
		int size = (map!=null)? map.size() : 0;
		TupleTypeInfo[] array = new TupleTypeInfo[size];
		int i = 0;
		if(map!=null)
		for(TupleTypeInfo r : map.values())
		{
			array[i] = r.copy();
			i++;
		}
		return array;
	}
	
	private TupleTypeInfo copy()
	{
			TupleTypeInfo r = new TupleTypeInfo(TSName,type);
			r.outCount = outCount;
			r.takeCount = takeCount;
			r.updateCount = updateCount;
			r.readCount = readCount;
			return r;
	}
	
	
	public static TupleTypeInfo getTypeRecord(String tsName,TupleType type){
		
		TupleTypeInfo tr = null;
		if(!maps.containsKey(tsName))
		{
		   maps.put(tsName, new HashMap<TupleType,TupleTypeInfo>());	
		}	
		HashMap<TupleType,TupleTypeInfo> records = maps.get(tsName);
		if(!records.containsKey(type))
		{
			tr = new TupleTypeInfo(tsName,type);
			
		    records.put(type, tr);
		}
		else
			tr =  records.get(type);
		return tr;
	}
	
	public TupleType getType()
	{
		return type;
	}
	public String getDracoTSName()
	{
		return TSName;
	}
	public long getOutCount()
	{
		return outCount;
	}
	
	public long getUpdateCount()
	{
		return updateCount;
	}

	public long getReadCount()
	{
		return readCount;
	}
	public synchronized void incrOutCount(long counts)
	{
		this.outCount += counts;
	}
	public synchronized void incrUpdateCount(long counts)
	{
		this.updateCount += counts;
	}
	
	public synchronized void incrReadCount(long counts)
	{
		this.readCount += counts;
	}
	
	
	public synchronized void incrTakeCount(long count) {

        takeCount  += count;
		
	}
	public String getTSName()
	{
		return TSName;
	}
	
	public TupleType getTupleType()
	{
		return type;
	}
	
	public Location getLocation()
	{
		return location;
	}
	
	public String getLocalString()
	{
		return String.format("%s : LOut %s, LUpdate %s", this.type,this.outCount,this.updateCount);
	}

	

	public long getTakeCount() {
		
		return takeCount;
	}

	private void initEstimatedNums() {
		
		estimatedNum = this.outCount;
		delTakeCount = this.takeCount - 0;
		delUpdateCount = this.updateCount - 0;
	}

	public void calEstimatedNums(TupleTypeInfo lastInfo) {
		
		if(lastInfo == null)
		{
			initEstimatedNums();
		}else
		{
		if(this.outCount >= lastInfo.outCount
			&& this.takeCount >= lastInfo.takeCount
			&& this.updateCount >= lastInfo.updateCount )
		{
			estimatedNum = lastInfo.estimatedNum +
				outCount - lastInfo.outCount -  lastInfo.delTakeCount;
			delTakeCount = this.takeCount - lastInfo.takeCount;
			delUpdateCount = this.updateCount - lastInfo.updateCount;
		}else
		{
			initEstimatedNums();
		}
		}
	}

	public long getEstimatedNum() {
		
		return estimatedNum;
	}

	public long getDelUpdateCount()
	{
		return delUpdateCount;
	}
	
	public String toString()
	{
		return "The type: " + type + " location: " + location 
			+ " , out count: " + outCount +	" , take count: " + takeCount;
	}
	
	public int getSize(){
		/*
		 * 	private long outCount = 0;
			private long updateCount = 0;
			private long delUpdateCount = 0;
		
			private TupleType type;
			private String TSName;
			private Location location = Location.HERE;

			private long takeCount = 0;
	
			public long delTakeCount = 0;
			public long estimatedNum=0;
			
			long: 8 Bytes
		 */
		return 8 * 6 + type.getTypeName().length() + TSName.length();  
	}
	
}
